{extend name="common/default" /}
{block name="style"}
<style>
    .weui-form__text-area{
        text-align: center;
        margin: 1rem;
    }
    .weui-form__opr-area{
        margin: 1rem;
    }
</style>
{/block}
{block name="container"}
<div class="container">
    <div class="page form_page js_show">
        <div class="weui-form">
            <div class="weui-form__text-area">
                <h2 class="weui-form__title">{$one.name}</h2>
            </div>
            <div class="weui-form__control-area">
                <div class="weui-cells__group weui-cells__group_form">
                    <div class="weui-cells weui-cells_form">
                        {foreach name="bind_info" item="vo"}
                        {if condition="$vo['typedata'] eq 0"}
                        <div class="weui-cell weui-cell_active">
                            <div class="weui-cell__hd"><label class="weui-label">{$vo['name']}</label></div>
                            <div class="weui-cell__bd">
                                <input class="weui-input" placeholder="{$vo['name']}" value="{$vo['value']}" name="bind{$vo['id']}" type="text">
                            </div>
                        </div>
                        {elseif condition="$vo['typedata'] eq 1"}
                        <div class="weui-cell weui-cell_active">
                            <div class="weui-cell__hd"><label class="weui-label">{$vo['name']}</label></div>
                            <div class="weui-cell__bd">
                                <input class="weui-input" placeholder="{$vo['name']}" value="{$vo['value']}" name="bind{$vo['id']}" type="number" pattern="[0-9]*">
                            </div>
                        </div>
                        {elseif condition="$vo['typedata'] eq 2"}
                        <div class="weui-cell weui-cell_active">
                            <div class="weui-cell__hd"><label class="weui-label">{$vo['name']}</label></div>
                            <div class="weui-cell__bd">
                                <input class="weui-input" placeholder="{$vo['name']}" value="{$vo['value']}" name="bind{$vo['id']}" type="date">
                            </div>
                        </div>
                        {elseif condition="$vo['typedata'] eq 3"}
                        <div class="weui-cell weui-cell_active">
                            <div class="weui-cell__hd"><label class="weui-label">{$vo['name']}</label></div>
                            <div class="weui-cell__bd">
                                <input class="weui-input" placeholder="{$vo['name']}" value="{$vo['value']}" name="bind{$vo['id']}" type="date">
                            </div>
                        </div>
                        {elseif condition="$vo['typedata'] eq 4"}
                        <div class="weui-cell" id="uploader">
                            <div class="weui-cell__bd">
                                <div class="weui-uploader">
                                    <div class="weui-uploader__hd">
                                        <p class="weui-uploader__title">{$vo['name']}</p>
                                    </div>
                                    <div class="weui-uploader__bd">
                                        <ul class="weui-uploader__files" id="uploaderFiles"></ul>
                                        <div class="weui-uploader__input-box">
                                            <input id="uploaderInput" class="weui-uploader__input" type="file" accept="image/*" capture="camera" multiple="" />
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        {elseif condition="$vo['typedata'] eq 6"}
                        <div class="weui-cells__title">{$vo['name']}</div>
                        <div class="weui-cells weui-cells_radio">
                            {volist name="vo['option']" key="k" id="v"}
                            <label class="weui-cell weui-check__label" for="r{$k}">
                                <div class="weui-cell__bd">{$v}</div>
                                <div class="weui-cell__ft">
                                    <input required type="radio" class="weui-check" name="bind{$vo['id']}" value="{$k}" id="r{$k}">
                                    <span class="weui-icon-checked"></span>
                                </div>
                            </label>
                            {/volist}
                        </div>
                        {/if}
                        {/foreach}
                    </div>
                </div>
            </div>
            <div class="weui-form__opr-area">
                <input type="hidden" name="rid" value="{$one.id}"/>
                <a class="weui-btn weui-btn_primary weui-btn_disabled" href="javascript:" id="submit">确定</a>
            </div>
        </div>
        <div id="js_toast" style="display: none;">
            <div class="weui-mask_transparent"></div>
            <div class="weui-toast">
                <i class="weui-icon-success-no-circle weui-icon_toast"></i>
                <p class="weui-toast__content">已完成</p>
            </div>
        </div>
    </div>
</div>
{/block}
{block name="javascript"}
<script type="text/javascript">
    function check_fill(){
        var fill = 0;
        var $input = $('input');
        $input.each(function(){
            if($(this).val())fill++;
        });
        console.log(fill,$input.length);
        if(fill == $input.length){
            $('#submit').removeClass('weui-btn_disabled');
        }else{
            $('#submit').addClass('weui-btn_disabled');
        }
    }

    $(function(){
        var $toast = $('#js_toast');
        var $input = $('input');
        $input.on('input', function(){
            check_fill();
        });
        $('#submit').on('click', function(){
            if ($(this).hasClass('weui-btn_disabled')) return;
            var data = {};
            $input.each(function(){
                eval("data." + $(this).attr('name') + "='"+$(this).val()+"'");
            });
            $.ajax({
                type: 'POST',
                url: "{:addon_url('rexam/rexam/bind')}",
                data: data,
                dataType: 'json',
                success: function (res) {
                    console.log(res);
                    if (res.code == 1) {
//                    window.location.href = "{:addon_url('rexam/index/index')}";
//                    history.back(-1);

                        // toptips的fixed, 如果有`animation`, `position: fixed`不生效
                        $('.page.cell').removeClass('slideIn');

                        $toast.fadeIn(100);
                        setTimeout(function () {
                            $toast.fadeOut(100);
                            window.location.href = "{$url}";
                        }, 2000);
                    }
                }
            })
        });
        check_fill();
    });
</script>
{/block}
